import csv
import parsel
import requests


class WeatherCrawler:

    def __init__(self, city_code="54511"):
        self.url = 'http://tianqi.2345.com/Pc/GetHistory'
        self.city_code = city_code
        self.params = {
            "areaInfo[areaId]": city_code,
            "areaInfo[areaType]": "2",
            "date[year]": "2023",
            "date[month]": "6"
        }
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.43',
            'Referer': 'http://tianqi.2345.com/wea_history/',
        }
        self.json_data = {}
        self.data_list = []

    def get_json(self):
        resp = requests.get(url=self.url, headers=self.headers, params=self.params)
        self.json_data = resp.json()

    def parse_json(self):
        html = self.json_data['data']
        selector = parsel.Selector(text=html)
        tr_list = selector.css('tr')
        for tr in tr_list:
            data = tr.css('td::text').getall()
            if data:
                data[0] = data[0].split(' ')[0]
                if self.city_code == '54511':
                    data.append('北京')
                elif self.city_code == '58362':
                    data.append('上海')
                elif self.city_code == '59493':
                    data.append('深圳')
                self.data_list.append(data)

    def save_data(self):
        with open('../天气数据分析/weather_data.csv', 'a', newline='', encoding='utf-8-sig') as f:
            csv_writer = csv.writer(f)
            csv_writer.writerows(self.data_list)

    def run(self):
        # 获取历史天气原始json数据
        self.get_json()
        # 解析出我想要的数据
        self.parse_json()
        # 保存解析后的数据
        self.save_data()


if __name__ == '__main__':
    with open('../天气数据分析/weather_data.csv', 'a', newline='', encoding='utf-8-sig') as f:
        csv_writer = csv.writer(f)
        csv_writer.writerow(['日期', '最高温', '最低温', '天气', '风力风向', '城市'])

    city_list = ['54511', '58362', '59493']
    for city_code in city_list:
        # 实例化一个天气爬虫
        wc = WeatherCrawler(city_code)
        # 启动爬虫
        wc.run()
